I need to install a new TeX/LaTeX/ConTeXt package on my computer. I'm running Ubuntu/Fedora/Slackware/Some-Version-Of Linux. What are the possible ways to do this?

Note: We get a number of questions that essentially reduce to this one; it was felt a good idea to have one question-and-answer that can cover all the standard methods. See Should 'how to install package X on Ubuntu' questions be consolidated? for the discussion. The question-and-answer are CW so that more information can be easily added. If you have information to add, please do so. If you are unsure, leave a comment here or (preferably) in chat. Comments on the question and answer will be periodically removed to ensure that the information is clear.

3 Answers
3

Method 1:

This is for if you are using the version of TeX that came with your distribution and you don't mind particularly which version of the package you install.

Linux distributions tend to include TeX as a collection of packages, but it isn't usually on as fine a level as one TeX package equals one Linux package. You therefore need to find the right distribution package containing the TeX package that you want and install it. Different package managers have different methods of searching, if all else fails there is often a website where you can search for a specific file:

How you install a package via TeX Live depends on whether you installed TeX Live as a single user or you installed it system-wide. If the first, try:

tlmgr install <package>

If the second, you will have a small problem in that tlmgr might not be in the path that sudo automatically searches. So you need to locate it first and use sudo to call it with the full path. If your shell has the which builtin, you can find tlmgr using the command which tlmgr. Thus:

which tlmgr
sudo /path/to/tlmgr install <package>

If your shell allows you to use the output of a command as a sort of variable, you can use the following command to condense the above (where we exploit the fact that which returns a single instance of the command regardless of how many times it appears on the path):

sudo $(which tlmgr) install <package>

(You can test your shell with the less risky echo $(which tlmgr): this should produce the same as which tlmgr.)

Method 3

Install manually

In this situation, you have two options: to install system-wide (all users on your system) or to install just for you. System-wide means that any user on your system will be able to use it, just for you means just that: it will only be installed for your account. The difference between the two is the base location:

System-wide: type kpsewhich -var-value TEXMFLOCAL in the Terminal to find the right directory

Just for you: type kpsewhich -var-value TEXMFHOME in the Terminal to find the right directory

One thing to note here is that in the case of a 'package clash', i.e., the same file is found in multiple places (such as the 'system' location, the 'local' location, and/or the 'individual user' location), only the first instance of the file will be used. In this case, the order of precedence is usually: individual user (TEXMFHOME) > local (TEXMFLOCAL) > system. The full list (in the right order) can be determined by running kpsewhich --var-value TEXINPUTS.

What happens now depends a bit on the package. If it is distributed in TDS then you are in luck. Go to the directory reported by kpsewhich, and into the tex subdirectory (or create it if it does not exist). Then extract the TDS file (remember to use sudo if globally, and check that your umask is set to 022). Note check the TDS file first to ensure that it will extract into the right place: you may need to move up or down a directory to get it to match.

If it is not distributed in TDS, then there are two options: If the package is available from TeX Live, you can get the TDS packaged version from http://mirror.ctan.org/systems/texlive/tlnet/archive/.tar.xz, and unpack it into a temporary directory. In most cases it will contain subdirectories like tex, fonts, etc. Copy these to the TEXMF tree as decided above. In case it contains a directory texmf-dist or similar, it is a package that also ships some scripts or different things outside the texmf hierarchy, in this case it is the best to contact an expert. Finally, you can also download the package from CTAN or the original distribution channel, unpack it and then there might be instructions - in which case follow them - but the general pattern is pretty similar. If it is a LaTeX package, create a directory in <base dir>/tex/latex/<package name> for it and extract the files (.sty plus perhaps .cls, etc.) there. Documentation files go in <base dir>/doc/latex/<package name>.

If you installed it globally, you will now need to run mktexlsr (or equivalently texhash) via sudo. If mktexlsr is not in the path that sudo searches, you will need to locate it. You can do:

which mktexlsr
sudo /path/to/mktexlsr

Or if your shell allows it:

sudo $(which mktexlsr)

This isn't necessary for local installations.

Notes and Supplementary

Note on sudo

When you add TeX Live to your path, it will not normally be added to the path for the root user, so will not be visible using sudo. The methods above use which to find tlmgr , etc., from the 'normal user' path and pass the correct instruction to sudo. This works with the bash and zsh shells: if you use another shell you may need to provide the full path to tlmgr instead.

When running sudo, the environment used is a mixture of the superuser's and the local user's. As well as the path mentioned above, it is important to ensure that the umask setting is sufficiently lax: a user should have a umask of 077 but root of 022. The umask controls the default permissions of newly created files: with 077 only root would be able to read (and thus use) them. With 022, everyone can read (and possibly execute) them.

If your system does not have sudo installed by default, it probably has it in its package repository. If not, see How universal is sudo? for alternatives.

Note on mktexlsr and texhash

To speed up its searching, the kpathsea library uses caches in the form of files called ls-R in various directories. These contain all the files that kpathseawould find if it searched properly in that directory (obeying any recursive search directives). The files are recreated by the mktexlsr command (which is short for "Make TeX's ls-R files") which has to be run manually (by someone with write access to the directory, usually root - see the note on sudo) if it is needed. This command used to be called texhash and that is still an alias for this command (technically, a symlink).

Note on kpsewhich

TeX uses the kpathsea library for finding its files. Where this looks depends on various variable which can be set in configuration files or by the user's shell. The list of directories that kpathsea searches in can be displayed by typing kpsewhich --var-value TEXINPUTS. The output is a little condensed. The following attempts to clarify it a little:

Paths are separated by colons (as in environment variables such as $PATH)

If a path begins with !! it means that the ls-R file will be consulted instead of the filesystem.

means that /usr/local/texlive/2012/texmf-dist and /usr/local/texlive/2012/texmf-var will both be searched, in that order. Both will actually use the cache which is stored in ls-R in the base of the search (thus /usr/local/texlive/2012/texmf-dist/ls-R, for example). And both searches will be recursive.

Testing the Installation

The simplest way to check that the installed file is being found is to run

kpsewhich <package_name>

(where <package_name> includes the extension). If this lists the newly installed file, all should be fine. If it doesn't but does display something then it may be that there is another version of this package installed which is being found first. If that is the case, then

kpsewhich --all <package_name>

will list all of the matching names so you can test to see if it is finding it at all.

If it did find it but isn't using it, you will either have to choose a location which is searched earlier than the version that is being found or remove the version(s) that are being found first.

If it did not find it at all, there are some possible reasons.

If you did a full installation of TeX Live, is the kpsewhich command from the new installation? Try which kpsewhich to find out. If not, adjust your $PATH and try again. (If your path is correct, it might be that your shell hasn't noticed it: try rehash to correct this (Bourne-Again Shells)).

If you only installed a package, did you put it somewhere that TeX can find it? The full list of directories in which TeX searches can be found by running

kpsewhich --var-value TEXMF

These are searched recursively.

If it is in a directory that TeX searches, and that directory is prepended in the list by !! then it is one where TeX consults the cache, not the actual files on the disc. To recreate the cache, run mktexlsr or texhash as super-user.

Dependent packages

If you do not use the package manager to install TeX or one of its packages (i.e., you didn't use Method 1), then the package manager will not know that what you have just installed is installed. This means that installing system packages that depend on these manually installed packages will try to install them again. The simplest solution is to let them do so as it is usually the case that your manually installed things are found before the system ones. If space is tight, many distributions have "dummy" packages that can be installed to trick the package manager. See How to install "vanilla" TeXLive on Debian or Ubuntu? for more details.

As I use Ubuntu I have installed TeX Live and of course I use apt-get to install packages.

I found that I was missing some files needed to compile my document. Using apt-cache as recommended in Method 1 above I found that texlive-latex-extra was the package I needed. However the download size was huge (327 MB).

The essential package only, was easily installed by

sudo apt-get install texlive-latex-extra --no-install-recommends

Including the --no-install-recommends avoids the huge (320 MB) of extras.