sodcalculator

10 Tech How-To

Install scikit-learn on Linux with pip or Conda

Guide 10-41491 · Updated · Step by step

To install scikit-learn on Linux, use pip in a Python virtual environment: run python3 -m venv .venv, activate it, then run python -m pip install -U scikit-learn. Conda or another compatible package manager is also an option; the right method depends on how Python is managed on your system.

What scikit-learn is and what you need

scikit-learn is a Python library for machine learning. It includes tools for tasks such as classification, regression, clustering, preprocessing, and model evaluation. The package is installed as scikit-learn, while Python code imports it using the name sklearn.

Before installing, check that Python 3 and a package manager are available. scikit-learn supports particular Python and dependency versions, and those requirements can change as releases advance. If installation fails because a dependency or Python version is unsupported, consult the current installation and compatibility information in the official scikit-learn documentation rather than assuming the newest release works with every system Python.

On Linux, you may have more than one Python interpreter: a system-managed one, a user-installed one, or one supplied by Conda. Installing into the wrong interpreter is a common source of confusion. A virtual environment helps keep the package and its dependencies separate from operating-system tools and other projects.

Install with pip in a virtual environment

Open a terminal in your project directory. The following commands create and activate a virtual environment, then install scikit-learn:

  1. Check Python: python3 --version. If that command is unavailable, check your Linux distribution’s documentation for its Python 3 package.
  2. Create an environment: python3 -m venv .venv.
  3. Activate it in Bash or a similar shell: source .venv/bin/activate. Your prompt may show (.venv) when it is active.
  4. Update pip inside the environment: python -m pip install --upgrade pip.
  5. Install the package: python -m pip install -U scikit-learn.

Using python -m pip ties pip to the Python interpreter named by python. This is more reliable than running a bare pip command when multiple Python installations exist. The -U option asks pip to upgrade the named package when appropriate; leave it out if you deliberately need to retain an installed version.

To leave the environment, enter deactivate. To use scikit-learn again later, go back to the project directory and run source .venv/bin/activate. The environment is local to that project; create a separate one for another project when you need different dependencies.

Choose pip, Conda, or your distribution’s packages

Use the package manager that owns the Python environment you intend to work in. Mixing package managers in one environment can make dependency changes harder to understand, so avoid installing the same library into a Conda environment with pip and Conda interchangeably unless you know why that is necessary.

  • pip and venv: A common choice for Python projects, especially when dependencies are recorded in a project file. It installs packages from Python package indexes into the active environment.
  • Conda: Useful if you already manage Python environments with Conda. Create or activate an environment, then install with conda install scikit-learn. Package availability and the exact command can depend on configured channels and your Conda setup.
  • Linux distribution package: Some distributions provide a system package for scikit-learn. This can integrate with system Python, but its version may differ from the latest upstream release. Use the distribution’s package manager and documentation, and do not use elevated privileges to run pip against system Python.

For a new project, a dedicated environment is usually the clearest option. If a course, employer, or existing project specifies Conda or a particular version, follow that environment’s instructions instead of creating a separate installation that the project will not use.

Verify the installation and use it

With the environment activated, check that Python can import the library and print its version:

python -c "import sklearn; print(sklearn.__version__)"

If a version number appears without an error, the active interpreter can import scikit-learn. You can also run a small example:

python -c "from sklearn.ensemble import RandomForestClassifier; print('scikit-learn is ready')"

In a script, import the specific tools you need from sklearn, for example from sklearn.model_selection import train_test_split. Do not try import scikit-learn: hyphens are not valid in Python import names. If you use Jupyter, make sure the notebook is using the same environment where you installed the package. A successful terminal import does not guarantee a notebook kernel points to that interpreter.

Common installation problems

  • python3 or venv is missing: Install the relevant Python 3 or virtual-environment component using your distribution’s package manager. Package names differ between Linux distributions, so check their documentation.
  • Permission denied: Do not respond by blindly adding sudo to pip. Create and activate a virtual environment, then install without administrator privileges.
  • The import fails after installation: Check that the environment is active and compare python -c "import sys; print(sys.executable)" with the interpreter used for installation. If necessary, run python -m pip show scikit-learn in that same environment.
  • Unsupported Python or dependency conflict: Check the full error and the compatibility requirements for the scikit-learn release you are installing. Use a supported Python version or adjust the project’s pinned dependencies; do not assume repeatedly upgrading every package is safe for an existing project.
  • Build or download errors: Confirm network access and that pip is running in the intended environment. Current compatible wheels are available for many common setups, but platform, Python, and package-version differences can affect installation.

Keep versions reproducible

A successful install today does not by itself ensure that another computer will get the same dependency versions. For a simple project, record dependencies with python -m pip freeze > requirements.txt. A future environment can install the recorded set with python -m pip install -r requirements.txt. Review that file before sharing it: pip freeze records packages in the current environment, including dependencies, not just scikit-learn.

For a project that needs a specific scikit-learn release, pin it explicitly in the project’s dependency configuration and test that combination. Avoid upgrading a working production or research environment without checking whether the change affects other packages or results. The documentation’s installation guidance and release information are the best references for current requirements and changes.

Frequently Asked Questions

How do I install scikit-learn on Ubuntu?
For a project-local installation, create a virtual environment with python3 -m venv .venv, activate it, and run python -m pip install -U scikit-learn. If venv support is missing, install the applicable Python component through Ubuntu’s package manager.

Is the package called sklearn or scikit-learn?
Install the package named scikit-learn. In Python code, import it using sklearn, as in import sklearn.

Should I use sudo to install scikit-learn?
Usually not for a project. Install it in a virtual environment owned by your user. Using administrator privileges with pip can interfere with packages managed by the Linux distribution.

How can I check whether scikit-learn is installed?
Activate the intended environment and run python -c "import sklearn; print(sklearn.__version__)". If it reports that the module is missing, check whether you installed it into a different Python environment.

Why does Jupyter say sklearn is not installed?
The notebook may be running a different Python interpreter from your terminal. Select or configure a notebook kernel that uses the environment where scikit-learn is installed, then try the import again.

Can I install scikit-learn with Conda?
Yes. Activate the Conda environment you want to use and run conda install scikit-learn. Check your environment’s configured channels and verify the import from that same environment afterward.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →