# Library
A collection of pre-written code, functions, classes, and routines that can be reused by developers to perform common tasks without writing the code from scratch.
**Pronunciation:** /ˈlaɪˌbrɛri/
**Difficulty:** Beginner
**Synonyms:** Module, Package
**Categories:** Software Development, Programming Concepts
**Tags:** Code Reuse, Modularity, Programming, Software Engineering, Dependency
Canonical: https://scaleengineer.com/glossaries/library
---
## Definitions

- **General Software Development:** In software development, a **library** is a collection of pre-written, reusable code—such as functions, classes, variables, and data structures—that can be invoked by other programs to perform specific tasks. Think of it as a toolkit for developers. Instead of building a common feature like handling HTTP requests or manipulating dates from scratch, a developer can simply use a function from a relevant library.

### Key Concepts
*   **Modularity**: Libraries help break down a large application into smaller, manageable, and independent modules.
*   **Reusability**: The primary benefit is code reuse, which saves time, reduces errors, and ensures consistency.
*   **Abstraction**: Libraries hide complex implementation details behind a simple interface (API), allowing developers to use powerful functionality without needing to understand its inner workings.

### Types of Libraries
*   **Static Library (.lib, .a)**: The library's code is copied directly into the final executable file during the compilation/linking phase. This makes the executable larger but self-contained.
*   **Dynamic Library / Shared Library (.dll, .so, .dylib)**: The library's code is not copied into the executable. Instead, the operating system loads it into memory at runtime when the program starts or when it's first needed. This results in smaller executables and allows multiple programs to share a single copy of the library in memory.

### Example
In Python, if you need to make a web request, you don't write the low-level networking code yourself. You would typically import the `requests` **library**:
```python
import requests

response = requests.get('https://api.example.com/data')
print(response.json())
```
Here, `requests` is a third-party **library** that abstracts away the complexity of making HTTP requests.

## Etymology

The term 'library' is borrowed from its real-world counterpart. It comes from the Latin 'librarium', meaning 'a place for books', which in turn derives from 'liber', meaning 'book'. In programming, a library serves as a 'place for code' that developers can 'check out' and use in their own projects, much like borrowing a book.

## First used

1950s

## Historical context

The concept of a library is nearly as old as programming itself, evolving alongside programming languages and development practices.

*   **1950s-1960s**: In the early days of computing with languages like Fortran, programmers began creating collections of subroutines for common mathematical and scientific calculations. These were often shared physically on punched cards or magnetic tapes, forming the first rudimentary libraries.

*   **1970s**: The rise of operating systems like Unix standardized the concept. The C programming language was introduced with a powerful standard C library (libc), which provided a baseline set of functions for input/output, string manipulation, and memory management. This established the paradigm of a language being accompanied by a 'standard library'.

*   **1980s-1990s**: Object-Oriented Programming (OOP) languages like C++ and Smalltalk furthered the concept by promoting the creation of class libraries, which bundled data and the methods to operate on that data into reusable components.

*   **1990s-2000s**: The explosion of the internet revolutionized library distribution. Centralized online repositories like CPAN (for Perl), PyPI (for Python), and Maven Central (for Java) emerged. These platforms, along with package managers (e.g., `pip`, `maven`), made it incredibly easy for developers worldwide to find, download, and manage third-party libraries, drastically accelerating development.

*   **2010s-Present**: Libraries are the fundamental building blocks of modern software. Ecosystems like npm (for JavaScript/Node.js) host millions of packages (a common synonym for libraries). This has enabled rapid application development but has also introduced new challenges, such as managing complex dependency trees ('dependency hell') and ensuring the security of third-party code.

## Q&A

- **What is the main difference between a static library and a dynamic library?:** A static library's code is copied into the application's executable file at compile time, making the executable larger but self-contained. A dynamic library is loaded by the operating system at runtime and can be shared by multiple applications, resulting in smaller executables.
- **Why would a developer choose to use a library instead of writing the code themselves?:** To save development time, reduce potential bugs by using well-tested code, and leverage specialized functionality without needing to be an expert in that specific domain. It promotes code reuse and modularity.
- **What is a 'standard library'?:** A standard library is the set of libraries that comes bundled with a programming language. It provides essential functionalities like input/output operations, data structures, and string manipulation without requiring the installation of third-party packages.

## Usage examples

- To work with dates and times in Python, I'll import the `datetime` **library**, which is part of the standard library.
- Our application's executable size was too large, so we switched from using a static **library** to a dynamic **module**.
- Instead of writing our own UI components, we decided to use the React **library** to build the front end of our web application.
- You need to add the `pandas` **package** as a dependency in your `requirements.txt` file to ensure it gets installed in the production environment.

## Related terms

- Framework
- API
- SDK
- Dependency Management
- Static Library
- Dynamic Library
- Package Manager

## Popular related terms

- Framework
- API
- SDK
